package edu.ccut.saturn.studio.validate.dialog;

import java.util.Map;

import org.eclipse.jface.dialogs.IDialogConstants;
import org.eclipse.jface.dialogs.TitleAreaDialog;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.ModifyEvent;
import org.eclipse.swt.events.ModifyListener;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;

import edu.ccut.saturn.studio.core.util.eclipse.Messages;
import edu.ccut.saturn.studio.core.util.eclipse.SaturnValidation;

public class ErrValidateDialog extends TitleAreaDialog {

	private Text textErrInformation;
	private Text textKey;
	private Map<String, String> errList;

	public ErrValidateDialog(Shell parentShell, Map<String, String> errList) {
		super(parentShell);
		this.errList = errList;
	}

	@Override
	protected Control createDialogArea(Composite parent) {
		Composite area = (Composite) super.createDialogArea(parent);
		Composite container = new Composite(area, SWT.NONE);
		final GridData gd_container = new GridData(SWT.FILL, SWT.FILL, false,
				false);
		gd_container.heightHint = 119;
		gd_container.widthHint = 135;
		container.setLayoutData(gd_container);
		final GridLayout gridLayout = new GridLayout();
		gridLayout.numColumns = 2;
		container.setLayout(gridLayout);

		final Label label = new Label(container, SWT.NONE);
		label.setText(Messages.getString("ErrValidateDialog.key")); //$NON-NLS-1$

		textKey = new Text(container, SWT.BORDER);
		textKey.setTextLimit(100);
		textKey.addModifyListener(new ModifyListener() {
			public void modifyText(final ModifyEvent e) {
				validateError();
			}
		});
		final GridData gd_textKey = new GridData(SWT.FILL, SWT.CENTER, true,
				false);
		textKey.setLayoutData(gd_textKey);

		final Label label_1 = new Label(container, SWT.NONE);
		final GridData gd_label_1 = new GridData();
		gd_label_1.verticalIndent = 5;
		label_1.setLayoutData(gd_label_1);
		label_1.setText(Messages.getString("ErrValidateDialog.errormessage")); //$NON-NLS-1$

		textErrInformation = new Text(container, SWT.BORDER);
		textErrInformation.setTextLimit(100);
		textErrInformation.addModifyListener(new ModifyListener() {
			public void modifyText(final ModifyEvent e) {
				validateError();
			}
		});
		final GridData gd_textErrInformation = new GridData(SWT.FILL,
				SWT.CENTER, true, false);
		gd_textErrInformation.verticalIndent = 5;
		textErrInformation.setLayoutData(gd_textErrInformation);
		setMessage(Messages
				.getString("ErrValidateDialog.installvalidatemessage")); //$NON-NLS-1$

		return area;
	}

	protected Point getInitialSize() {
		return new Point(493, 230);
	}

	protected void buttonPressed(int buttonId) {
		if (buttonId == IDialogConstants.OK_ID) {
			this.errList.put(textKey.getText().trim(), textErrInformation
					.getText().trim());
		}
		super.buttonPressed(buttonId);
	}

	protected void createButtonsForButtonBar(Composite parent) {
		createButton(parent, IDialogConstants.OK_ID, Messages
				.getString("ManageConnectionDialog.confirm"), false); //$NON-NLS-1$
		this.getButton(IDialogConstants.OK_ID).setEnabled(false);
		createButton(parent, IDialogConstants.CANCEL_ID, Messages
				.getString("ManageConnectionDialog.cancel"), false); //$NON-NLS-1$
	}

	protected boolean validateError() {
		String key = textKey.getText().trim();
		String errInformation = textErrInformation.getText().trim();

		if (SaturnValidation.isEmpty(key)) {
			setErrorMessage(Messages.getString("ErrValidateDialog.MaxLength")); //$NON-NLS-1$
			this.getButton(IDialogConstants.OK_ID).setEnabled(false);
			return false;
		}

		if (SaturnValidation.isBadWord(key)) {
			setErrorMessage(Messages.getString("ErrValidateDialog.Badword")); //$NON-NLS-1$
			this.getButton(IDialogConstants.OK_ID).setEnabled(false);
			return false;
		}

		if (SaturnValidation.isEmpty(errInformation)) {
			setErrorMessage(Messages.getString("ErrValidateDialog.IsEmpty")); //$NON-NLS-1$
			this.getButton(IDialogConstants.OK_ID).setEnabled(false);
			return false;
		}

		setErrorMessage(null);
		this.getButton(IDialogConstants.OK_ID).setEnabled(true);
		return true;

	}
	protected void configureShell(Shell newShell) {
		super.configureShell(newShell);
		newShell.setText(Messages.getString("ErrValidateDialog.addParameter")); //$NON-NLS-1$
	}
}
